package org.rsbot.bot;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.rsbot.util.GlobalConfiguration;

public class Crawler
{
  private static final Logger log = Logger.getLogger(Crawler.class.getName());
  private static HashMap<String, String> parameters;
  private final String world_prefix;

  public Crawler(String paramString)
  {
    String str1 = firstMatch("<a id=\"continue\" class=\"barItem\" href=\"([^\"]+)\"\\s+onclick=\"[^\"]+\">Continue to Full Site for News and Game Help", downloadPage(paramString, null));

    String str2 = new StringBuilder().append(paramString).append("game.ws").toString();

    String str3 = firstMatch("<frame id=\"[^\"]+\" style=\"[^\"]+\" src=\"([^\"]+)\"", downloadPage(str2, str1));

    this.world_prefix = str3.substring(12, str3.indexOf(".runescape"));

    Pattern localPattern = Pattern.compile("<param name=\"?([^\\s]+)\"?\\s+value=\"?([^>]*)\"?>", 66);

    Matcher localMatcher = localPattern.matcher(downloadPage(str3, str2));
    parameters = new HashMap();
    while (localMatcher.find()) {
      String str4 = removeTrailingChar(localMatcher.group(1), '"');
      String str5 = removeTrailingChar(localMatcher.group(2), '"');
      if (!parameters.containsKey(str4)) {
        parameters.put(str4, str5);
      }

    }

    if (parameters.containsKey("haveie6")) {
      parameters.remove("haveie6");
    }
    parameters.put("haveie6", "0");

    log.fine(new StringBuilder().append("Parameters: ").append(parameters).toString());
  }

  private String downloadPage(String paramString1, String paramString2) {
    try {
      BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(GlobalConfiguration.getURLConnection(new URL(paramString1), paramString2).getInputStream()));

      StringBuilder localStringBuilder = new StringBuilder();
      String str;
      while ((str = localBufferedReader.readLine()) != null) {
        localStringBuilder.append(str);
      }
      localBufferedReader.close();
      return localStringBuilder.toString();
    } catch (Exception localException) {
      localException.printStackTrace();
    }
    return null;
  }

  private String firstMatch(String paramString1, String paramString2) {
    Pattern localPattern = Pattern.compile(paramString1);
    Matcher localMatcher = localPattern.matcher(paramString2);
    if (localMatcher.find())
      return localMatcher.group(1);
    return null;
  }

  public HashMap<String, String> getParameters() {
    return parameters;
  }

  public String getWorldPrefix() {
    return this.world_prefix;
  }

  private String removeTrailingChar(String paramString, char paramChar) {
    if ((paramString == null) || (paramString.isEmpty()))
      return paramString;
    if (paramString.length() == 1)
      return paramString.charAt(0) == paramChar ? "" : paramString;
    try {
      int i = paramString.length() - 1;
      if (paramString.charAt(i) == paramChar)
        return paramString.substring(0, i);
      return paramString; } catch (Exception localException) {
    }
    return paramString;
  }
}